package com.sixth.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.Medicines;
import com.sixth.core.entity.Producer;
import com.sixth.core.entity.Provider;
import com.sixth.core.entity.User;
import com.sixth.core.feign.UserFeign;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import com.sixth.stock.mapper.ProviderMapper;
import com.sixth.stock.service.ProviderService;
import com.sixth.stock.vo.MedicinesVo;
import com.sixth.stock.vo.ProviderVo;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【stock_provider(供应商信息表)】的数据库操作Service实现
* @createDate 2024-08-14 19:56:44
*/
@Service
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider>
    implements ProviderService{
@Resource
private ProviderMapper providerMapper;
@Resource
private UserFeign userFeign;
    @Override
    public R<IPage<Provider>> selectByPage(ProviderVo providerVo)  {
        Integer pageNum = providerVo.getPageNum();
        Integer pageSize = providerVo.getPageSize();
        if (pageNum==null||pageNum<=0||pageSize==null||pageSize<=0){
            pageNum=1;
            pageSize=5;
        }
        IPage<Provider> page =new Page<>(pageNum,pageSize);
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        /*pageNum: 1,
        pageSize: 5,
        providerName: undefined,
        contactName: undefined,
        contactTel: undefined,
        status: undefined*/
        if (providerVo.getProviderName()!=null&&providerVo.getProviderName()!=""){
            wrapper.like("provider_name",providerVo.getProviderName());
        }
        if (providerVo.getContactName()!=null&&providerVo.getContactName()!=""){
            wrapper.like("contact_name",providerVo.getContactName());
        }
        if (providerVo.getContactTel()!=null&&providerVo.getContactTel()!=""){
            wrapper.like("contact_tel",providerVo.getContactTel());
        }
        if (providerVo.getStatus()!=null&&providerVo.getStatus()!=""){
            wrapper.eq("status",providerVo.getStatus());
        }
        if (providerVo.getStatus()==null||providerVo.getStatus()==""){
            wrapper.eq("status","0");
        }
        wrapper.eq("del_flag",0);//查询未删除的数据
        wrapper.orderByDesc("create_time");//根据创建时间降序排序
        IPage<Provider> iPage = providerMapper.selectPage(page, wrapper);
        return R.ok(iPage);
    }

    @Override
    public R<IPage<Provider>> selectInit() {
        Page<Provider> page=new Page<>(1, 5);
        QueryWrapper<Provider> wrapper=new QueryWrapper<>();
        wrapper.eq("status","0");
        wrapper.eq("del_flag","0");//查询未删除的数据
        wrapper.orderByDesc("create_time");//根据创建时间降序排序
        IPage<Provider> providerIPage = providerMapper.selectPage(page, wrapper);
        return R.ok(providerIPage);
    }

    @Override
    public R update(Provider provider) {
        String username = WebUtil.getUsername();
        User data = userFeign.getByPhone(username).getData();
        username = data.getUserName();
        provider.setUpdateBy(username);
        provider.setUpdateTime(new Date());
        int i = providerMapper.updateById(provider);
        return i>0?R.ok():R.error("更新失败");
    }

    @Override
    public R add(Provider provider) {
        String username = WebUtil.getUsername();User data = userFeign.getByPhone(username).getData();
        username = data.getUserName();
        provider.setCreateBy(username);
        provider.setCreateTime(new Date());
        provider.setDelFlag("0");
        int i = providerMapper.insert(provider);
        return i>0?R.ok():R.error("添加失败");
    }

    @Override
    public R<Provider> getProvider(Long id) {
        Provider provider = providerMapper.selectById(id);
        return provider!=null?R.ok(provider):R.error("查询失败");
    }
    @Override
    public R<List<Provider>> listProvider() {
        List<Provider> providers = providerMapper.selectList(new QueryWrapper<Provider>().eq("del_flag", 0).eq("status", 0));
        return R.ok(providers);
    }

    @Override
    public R batchDelete(Long[] ids) {
        // 批修改
        for (Long id : ids) {
            Provider provider = providerMapper.selectById(id);
            if (provider != null) {
                provider.setDelFlag("1");
                providerMapper.updateById(provider);
            }
        }
        return R.ok();
    }

    @Override
    public R deleteOne(Long id) {
        Provider provider = providerMapper.selectById(id);
        if (provider != null) {
            provider.setDelFlag("1");
            providerMapper.updateById(provider);
        }
        else {
            return R.error("删除失败,该供应商不存在");
        }
        return  R.ok();
    }
}




